/* eslint-disable no-unused-vars */
console.log('content-script!')

const NavListMapper = {
  getNavList () {
    return JSON.parse(localStorage.getItem('navList') || '[]')
  },
  storeNavList (navList) {
    localStorage.setItem('navList', JSON.stringify(navList))
  },
  addNav (nav) {
    let navList = this.getNavList()
    let include = false
    for (const item of navList) {
      if (item.title === nav.title) {
        include = true
      }
    }
    if (!include) {
      navList.push(nav)
      this.storeNavList(navList)
    }
  }
}

var $ = window.$

/**
   * 查询页面下载链接
   * @returns {Array} 返回拼装了域名并URL编码后的下载链接，示例: http://alist.xiaoya.pro/d/%E9%9F%B3%E4%B9%90
   */
const startDownload = function () {
  findAllNav()

  // 依次点击二级导航，显示出未显示的nav
  $('#content > div.d-flex.slider-menu-father.mb-4 > div > ul > li > a').each(function (index, item) {
    let jqObj = $(item).html(`<span>${$(item).text()}</span>`)
    jqObj.find('span').click()
    findAllNav()
  })
}

// 查找整个页面当前显示的所有导航
function findAllNav () {
  $('.url-card').each((index, item) => {
    // console.log($(item))
    // console.log($(item).find('div.url-info.flex-fill > div > strong'))
    let navTitle = $(item).find('div.url-info.flex-fill > div > strong').html()
    // console.log('navTitle: ' + navTitle)

    let navDesc = $(item).find('div.url-info.flex-fill > p').html()
    // console.log('navDesc: ' + navDesc)

    let iconUrl = $(item).find('div.url-img > img').attr('src')
    // console.log('navIconUrl: ' + iconUrl)
    let nav = {
      title: navTitle,
      desc: navDesc,
      iconUrl: iconUrl
    }
    // console.log('nav: ', nav)

    NavListMapper.addNav(nav)

    // sleep(1000)

    console.log($(item).find('.card-body'))
    $(item).find('.card-body').click()
  })
}

function sleep (time) {
  var timeStamp = new Date().getTime()
  var endTime = timeStamp + time
  while (true) {
    if (new Date().getTime() > endTime) {
      return
    }
  }
}

function parseDetailPage () {
  let navTitle = $('#content > div.row.site-content.py-4.py-md-5.mb-xl-5.mb-0.mx-xxxl-n5 > div.col.mt-4.mt-sm-0 > div > h1').html()
  console.log('navTitle: ' + navTitle)

  let siteUrl = $('#content > div.row.site-content.py-4.py-md-5.mb-xl-5.mb-0.mx-xxxl-n5 > div.col.mt-4.mt-sm-0 > div > div > div.site-go.mt-3 > span > a').attr('href')
  console.log('siteUrl: ' + siteUrl)

  let otherSite = []
  $('#content > div.row.site-content.py-4.py-md-5.mb-xl-5.mb-0.mx-xxxl-n5 > div.col.mt-4.mt-sm-0 > div > div > div.spare-site.mb-3 > a').each((index, item) => {
    let otherSiteUrl = $(item).attr('href')
    let otherSiteTitle = $(item).find('span').text()
    otherSite.push({
      title: otherSiteTitle,
      url: otherSiteUrl
    })
  })
  console.log('otherSite: ', otherSite)

  let navLongDesc = $('#content > main > div > div > div.panel.site-content.card.transparent > div > div.panel-body.single.my-4 > p').text()
  console.log('navLongDesc: ' + navLongDesc.trim())

  // 读取storage中的nav数据，补充直达链接等数据
  let navList = NavListMapper.getNavList()
  for (const item of navList) {
    if (item.title === navTitle) {
      item.siteUrl = siteUrl
      item.otherSite = otherSite
    }
  }
  NavListMapper.storeNavList(navList)

  console.log(chrome)
  chrome.runtime.sendMessage('closeCurrentTab')
}

console.log(window)

$(function () {
  if ((/https:\/\/ai-bot.cn\/sites\/.*\.html/g).test(location.href)) {
    parseDetailPage()
  }
  if ((/https:\/\/ai-bot.cn/g).test(location.href)) {
    setTimeout(() => {
      startDownload()
    }, 3000)
  }
})
